
package com.android.finaltracer.model;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.android.finaltracer.model.FTDataProvider.RelationColumns;

public class FTProviderInterface {

    private Context mContext;
    private static FTProviderInterface instance;

    private FTProviderInterface(Context context) {
        mContext = context;
    }

    public static FTProviderInterface getInstance(Context context) {
        if (instance == null) {
            instance = new FTProviderInterface(context);
        }
        return instance;
    }

    public ArrayList<String> getAllRelationNumber(String selection) {
        ArrayList<String> result = new ArrayList<String>();
        Cursor mCursor = null;
        try {
            mCursor = this.getQuery(
                    "content://com.android.ft.provider.data/relations", null,
                    selection, null, null);
            if (mCursor.moveToFirst()) {
                do {
                    String tmpPhone = mCursor.getString(mCursor
                            .getColumnIndex(RelationColumns.NUMBER));
                    if (tmpPhone != null)
                        result.add(tmpPhone);
                } while (mCursor.moveToNext());
            }
        } catch (Exception e) {
            // Error
        } finally {
            if (mCursor != null)
                mCursor.close();
        }
        return result;
    }

    public boolean saveRelationNumber(ContentValues values) {
        boolean result = false;
        boolean isExist = false;
        int existID = 0;
        Cursor mCursor = null;
        try {
            mCursor = this.getQuery(
                    "content://com.android.ft.provider.data/relations", null,
                    null, null, null);
            if (mCursor != null && mCursor.moveToFirst()) {
                do {
                    String number = mCursor.getString(mCursor
                            .getColumnIndex(RelationColumns.NUMBER));
                    if (values.containsKey(RelationColumns.NUMBER)
                            && number.equals(values
                                    .getAsString(RelationColumns.NUMBER))) {
                        isExist = true;
                        existID = mCursor.getInt(mCursor
                                .getColumnIndex(RelationColumns._ID));
                        break;
                    }
                    String imei = mCursor.getString(mCursor
                            .getColumnIndex(RelationColumns.IMEI));
                    if (values.containsKey(RelationColumns.IMEI)
                            && imei != null
                            && imei.equals(values
                                    .getAsString(RelationColumns.IMEI))) {
                        isExist = true;
                        existID = mCursor.getInt(mCursor
                                .getColumnIndex(RelationColumns._ID));
                        break;
                    }
                } while (mCursor.moveToNext());
            }
            if (isExist && existID > 0) {
                Uri mUri = Uri
                        .parse("content://com.android.ft.provider.data/relations/"
                                + existID);
                mContext.getContentResolver().update(mUri, values, null, null);
            } else {
                Uri mUri = Uri
                        .parse("content://com.android.ft.provider.data/relations");
                Uri error = mContext.getContentResolver().insert(mUri, values);
                result = (error == null) ? false : true;
            }
        } catch (Exception e) {
            result = false;
        } finally {
            if (mCursor != null)
                mCursor.close();
        }
        return result;
    }

    public boolean setGpsOn(String number, boolean on) {
        boolean result = false;
        if (number == null)
            return result;
        try {
            ContentValues values = new ContentValues();
            values.put(RelationColumns.NUMBER, number);
            if (on) {
                values.put(RelationColumns.GPS_ON, 2);
            } else {
                values.put(RelationColumns.GPS_ON, 1);
            }
            result = saveRelationNumber(values);
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    public boolean getGpsOn(String number) {
        boolean result = false;
        if (number == null)
            return result;
        try {
            Cursor mCursor = this.getQuery(
                    "content://com.android.ft.provider.data/relations", null,
                    RelationColumns.GPS_ON + ">" + 1, null, null);
            if (mCursor.moveToFirst()) {
                do {
                    String tmpNumber = mCursor.getString(mCursor
                            .getColumnIndex(RelationColumns.NUMBER));
                    if (number.equals(tmpNumber)) {
                        result = true;
                        break;
                    }
                } while (mCursor.moveToNext());
            }
        } catch (Exception e) {
            result = false;
        }

        return result;
    }

    private Cursor getQuery(String path, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        Cursor result = null;
        Uri mUri = Uri.parse(path);
        result = mContext.getContentResolver().query(mUri, projection,
                selection, selectionArgs, sortOrder);
        return result;
    }

    public void clearWaitBindSign(String phoneNum) {
        ContentValues values = new ContentValues();
        values.put(RelationColumns.WEIGHT_TYPE,
                RelationColumns.WEIGHT_TYPE_UNBIND);
        String selection = RelationColumns.WEIGHT_TYPE + "='"
                + RelationColumns.WEIGHT_TYPE_WAIT_BIND + "'";
        if (phoneNum != null) {
            selection += " AND " + RelationColumns.NUMBER + "='" + phoneNum
                    + "'";
        }
        mContext.getContentResolver().update(
                Uri.parse("content://com.android.ft.provider.data/relations"),
                values, selection, null);
    }

}
